/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.user.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import link.chengguo.orangemall.logging.aop.log.Log;
import link.chengguo.orangemall.modules.aop.ForbidSubmit;
import link.chengguo.orangemall.modules.user.domain.OrangeUser;
import link.chengguo.orangemall.modules.user.domain.OrangeUserLevel;
import link.chengguo.orangemall.modules.user.service.OrangeUserLevelService;
import link.chengguo.orangemall.modules.user.service.OrangeUserService;
import link.chengguo.orangemall.modules.user.service.dto.UserMoneyDto;
import link.chengguo.orangemall.modules.user.service.dto.OrangeUserQueryCriteria;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @author chengguo
* @date 2019-10-06
*/
@Api(tags = "商城:会员管理")
@RestController
@RequestMapping("api")
public class MemberController {

    private final OrangeUserService orangeUserService;

    private final OrangeUserLevelService userLevelService;

    public MemberController(OrangeUserService orangeUserService, OrangeUserLevelService userLevelService) {
        this.orangeUserService = orangeUserService;
        this.userLevelService = userLevelService;
    }

    @Log("查看下级")
    @ApiOperation(value = "查看下级")
    @PostMapping(value = "/orangeUser/spread")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_EDIT')")
    public ResponseEntity getSpread(@RequestBody OrangeUserQueryCriteria criteria){
        return new ResponseEntity<>(orangeUserService.querySpread(criteria.getUid(),criteria.getGrade()),
                HttpStatus.OK);
    }

    @Log("查询用户")
    @ApiOperation(value = "查询用户")
    @GetMapping(value = "/orangeUser")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_SELECT')")
    public ResponseEntity getOrangeUsers(OrangeUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orangeUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @Log("修改用户")
    @ApiOperation(value = "修改用户")
    @PutMapping(value = "/orangeUser")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_EDIT')")
    public ResponseEntity update(@Validated @RequestBody OrangeUser resources){
        orangeUserService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除用户")
    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/orangeUser/{uid}")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_DELETE')")
    public ResponseEntity delete(@PathVariable Integer uid){
        orangeUserService.removeById(uid);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ForbidSubmit
    @ApiOperation(value = "用户禁用启用")
    @PostMapping(value = "/orangeUser/onStatus/{id}")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_EDIT')")
    public ResponseEntity onStatus(@PathVariable Long id,@RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Integer status = jsonObject.getInteger("status");
        orangeUserService.onStatus(id,status);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ForbidSubmit
    @ApiOperation(value = "用户认证状态修改")
    @PostMapping(value = "/orangeUser/authType/{id}")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_EDIT')")
    public ResponseEntity onAuthType(@PathVariable Long id,@RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Integer authType = jsonObject.getInteger("authType");
        orangeUserService.onAuthType(id,authType);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ForbidSubmit
    @ApiOperation(value = "用户认证状态修改")
    @PostMapping(value = "/orangeUser/level/{id}")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_EDIT')")
    public ResponseEntity onlevel(@PathVariable Long id,@RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Integer level = jsonObject.getInteger("level");
        userLevelService.remove(new QueryWrapper<OrangeUserLevel>().eq("uid",id));
        userLevelService.setLevel(id,level);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "修改余额")
    @PostMapping(value = "/orangeUser/money")
    @PreAuthorize("hasAnyRole('admin','ORANGEUSER_ALL','ORANGEUSER_EDIT')")
    public ResponseEntity updatePrice(@Validated @RequestBody UserMoneyDto param){
        orangeUserService.updateMoney(param);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

}
